package com.bw.a2209clubshop.app

import android.app.Activity
import android.app.Application
import android.content.res.Configuration
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.a2209clubshop.AppConfig
import com.bw.a2209clubshop.mmkv.AppMMKV
import com.bw.a2209clubshop.mmkv.AuthMMKV
import com.bw.a2209clubshop.mmkv.UserMMKV
import com.bw.a2209clubshop.ui.login.model.UserInfo
import com.tencent.mmkv.BuildConfig
import com.tencent.mmkv.MMKV
import dagger.hilt.android.HiltAndroidApp
import me.jessyan.autosize.AutoSizeConfig
import me.jessyan.autosize.onAdaptListener
import me.jessyan.autosize.utils.ScreenUtils

@HiltAndroidApp
class App:Application() {
    companion object {
        private lateinit var app: App
        fun getAppContext(): App {
            return app
        }
    }

    override fun onCreate() {
        super.onCreate()
        app = this

        MMKV.initialize(this)

        AppMMKV.needGuide = false
        UserMMKV.hasBindDevice = true
        AuthMMKV.token = "1234567"
        UserMMKV.userInfo = UserInfo("123","hahaha")

        initAutoSize()
        initARouter()
    }
    /**
     * ARouter初始化
     */
    private fun initARouter() {
        if (BuildConfig.DEBUG) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(this)
//        EventBusInitializer.init(this)
    }
    /**
     * 屏幕适配
     */
    private fun initAutoSize() {
        AutoSizeConfig.getInstance()
            .setLog(BuildConfig.DEBUG)
            .setPrivateFontScale(1f)
            .onAdaptListener = object : onAdaptListener {
            override fun onAdaptBefore(target: Any?, activity: Activity?) {
                AutoSizeConfig.getInstance().screenWidth = ScreenUtils.getScreenSize(activity)[0]
                AutoSizeConfig.getInstance().screenHeight = ScreenUtils.getScreenSize(activity)[1]
                if (activity?.resources?.configuration?.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    AutoSizeConfig.getInstance().designHeightInDp = AppConfig.DESIGN_WIDTH_IN_DP
                    AutoSizeConfig.getInstance().designWidthInDp = AppConfig.DESIGN_HEIGHT_IN_DP
                } else {
                    AutoSizeConfig.getInstance().designHeightInDp = AppConfig.DESIGN_HEIGHT_IN_DP
                    AutoSizeConfig.getInstance().designWidthInDp = AppConfig.DESIGN_WIDTH_IN_DP
                }
            }

            override fun onAdaptAfter(target: Any?, activity: Activity?) {

            }
        }
    }
}